import { ref, computed, inject, unref } from 'vue';

/**
 * 表格操作按钮相关hook
 * 处理表格操作按钮和权限控制
 * @param {Object} options - 配置选项
 * @returns {Object} - 返回操作按钮相关状态和方法
 */
export function useTableActions(options) {
  const {
    actions = [], // 操作按钮配置
    selectedRowKeys = ref([]), // 选中行的键值
    selectedRows = ref([]), // 选中的行数据
    checkPermission = null, // 权限检查函数
  } = options || {};

  // 尝试从provide/inject获取权限检查函数
  const injectCheckPermission = inject('checkPermission', null);
  
  // 实际使用的权限检查函数
  const actualCheckPermission = checkPermission || injectCheckPermission || ((permission) => true);

  // 过滤有权限的操作按钮
  const filteredActions = computed(() => {
    return actions.filter(action => {
      // 检查权限
      if (action.permission && !actualCheckPermission(action.permission)) {
        return false;
      }
      
      // 检查条件
      if (action.condition && typeof action.condition === 'function') {
        return action.condition(unref(selectedRowKeys), unref(selectedRows));
      }
      
      return true;
    });
  });

  // 分组操作按钮
  const groupedActions = computed(() => {
    const groups = {};
    
    filteredActions.value.forEach(action => {
      const group = action.group || 'default';
      if (!groups[group]) {
        groups[group] = [];
      }
      groups[group].push(action);
    });
    
    return groups;
  });

  // 操作按钮位置分组
  const actionsByPosition = computed(() => {
    const positions = {
      left: [],
      right: [],
      dropdown: [],
      row: [],
    };
    
    filteredActions.value.forEach(action => {
      const position = action.position || 'left';
      positions[position].push(action);
    });
    
    return positions;
  });

  // 检查是否禁用操作按钮
  const isActionDisabled = (action) => {
    // 批量操作但未选择
    if (action.batchAction && (!selectedRowKeys.value || selectedRowKeys.value.length === 0)) {
      return true;
    }
    
    // 单行操作但选择了多行或未选择
    if (action.singleAction && (!selectedRowKeys.value || selectedRowKeys.value.length !== 1)) {
      return true;
    }
    
    // 自定义禁用条件
    if (action.disabled && typeof action.disabled === 'function') {
      return action.disabled(selectedRowKeys.value, selectedRows.value);
    }
    
    return false;
  };

  // 处理操作按钮点击
  const handleAction = (action, record = null) => {
    // 如果提供了记录，使用记录ID作为选中值
    const keys = record ? [record.id] : selectedRowKeys.value;
    const rows = record ? [record] : selectedRows.value;
    
    // 如果提供了处理函数，调用处理函数
    if (action.handler && typeof action.handler === 'function') {
      return action.handler(keys, rows, action);
    }
    
    // 使用动态调用
    if (action.method && typeof action.method === 'string') {
      const method = options[action.method];
      if (method && typeof method === 'function') {
        return method(keys, rows, action);
      }
    }
    
    return null;
  };

  // 获取操作按钮属性
  const getActionProps = (action) => {
    const props = { ...(action.props || {}) };
    
    // 设置禁用状态
    props.disabled = isActionDisabled(action);
    
    // 设置危险状态
    if (action.danger) {
      props.danger = true;
    }
    
    return props;
  };

  // 获取行操作按钮
  const getRowActions = (record) => {
    return filteredActions.value
      .filter(action => action.position === 'row')
      .filter(action => {
        // 检查条件
        if (action.condition && typeof action.condition === 'function') {
          return action.condition([record.id], [record]);
        }
        return true;
      });
  };

  return {
    // 状态
    filteredActions,
    groupedActions,
    actionsByPosition,
    
    // 方法
    isActionDisabled,
    handleAction,
    getActionProps,
    getRowActions,
  };
}
